package xyz.fefine;

import java.util.List;

/**
 * Created by feng_ on 2016/9/11.
 */
public class ParameterList extends ASTList {

    protected int[] offsets = null;

    public ParameterList(List<ASTree> list) {
        super(list);
    }

    public String name(int i) {
        return ((ASTLeaf)child(i)).token().getString();
    }

    public int size(){
        return numChildren();
    }

    /**
     * 将参数放入函数的执行环境中
     * @param enviorment
     * @param index
     * @param value
     * @return
     */
    public Object eval(Enviorment enviorment, int index, Object value) {
//        enviorment.putNew(name(index), value);
        //通过索引进行放值
        enviorment.put(0,offsets[index],value);
        return value;
    }

    @Override
    public void lookup(Symbols syms) {
        int s = size();
        offsets = new int[s];
        for (int i = 0; i < s; i++) {
            offsets[i] = syms.putNew(name(i));
        }
//        super.lookup(syms);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("(");
        for (ASTree asTree : super.children) {
            sb.append(((Name) asTree).name()+",");
        }
        sb.append(")");
        return sb.toString();
    }
}
